package com.cat.controller;

import com.cat.pojo.Admin;
import com.cat.service.AdminService;
import com.cat.util.LayUI;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class adminController {
    @Resource
    private AdminService adminService;
    @PostMapping("/tologin")
    @ResponseBody
    public Admin   login(@RequestBody Admin admin,HttpServletRequest request){

        Admin admin1 = adminService.login(admin.getAdmin_name(), admin.getAdmin_pass());
        if(admin1!=null){
            System.out.println(admin1);
            request.getSession().setAttribute("name",admin1.getAdmin_name());
           return admin1;
        }
       // return new ModelAndView("/index.html");
      //  return new ModelAndView("/login.html");
        return null;
    }
    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request){
        System.out.println("1111111111111111111");
         request.getSession().invalidate();
        System.out.println("222222222222222222222");

        return new ModelAndView("/index.html");
    }
    @GetMapping("/admin/getAllAdmin")
    @ResponseBody
    public LayUI getAllAdmin(){
        LayUI lu=new LayUI();
        lu.setCode(0);
        lu.setMsg("");
        lu.setData(adminService.getall());
        return  lu;
    }

}
